package fun.flyee.android.pengshan.acts;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.media.Ringtone;
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import butterknife.BindView;
import butterknife.OnClick;
import fun.flyee.android.pengshan.AppContex;
import fun.flyee.android.pengshan.utils.Logger;
import fun.flyee.android.pengshan.utils.NotificationUtils;
import fun.flyee.pengshan.social.R;

public abstract class BaseNotifyActivity extends BaseActivity {

    private final static String TAG = "floatAction";

    private Notification notification;

    private int notifyId = 1001;
    protected boolean show = true;

    protected Ringtone ring;

//    protected boolean mIsSmall = false; // 当前是否小窗口
//    private float mLastTx = 0; // 手指的上一个位置
//    private float mLastTy = 0;

    @Override
    protected void onCreate(Bundle b) {
        super.onCreate(b);

    }

    @Override
    public void finish() {
        show = false;
        super.finish();
    }

    protected void showNotify() {
        if (!show) return;
        Intent intent = new Intent(this, this.getClass());
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        if (Build.VERSION.SDK_INT >= 26) {
            NotificationUtils notificationUtils = new NotificationUtils(AppContex.getContext());
            Notification.Builder builder = notificationUtils.getAndroidChannelNotification("通话中...", "点击回到通话界面");
            builder.setSmallIcon(R.mipmap.ic_speak);
            builder.setAutoCancel(false);
            builder.setContentIntent(pendingIntent);
            notification = builder.build();
            notificationUtils.show(notifyId, notification);
        } else {
            Notification.Builder builder = new Notification.Builder(this);
            builder.setContentTitle("通话中...") // 设置下拉列表里的标题
                    .setAutoCancel(false)
                    .setSmallIcon(R.mipmap.ic_speak) // 设置状态栏内的小图标
                    .setSound(null)
                    .setContentIntent(pendingIntent)
                    .setContentText("点击回到通话界面") // 设置上下文内容
                    .setWhen(System.currentTimeMillis()); // 设置该通知发生的时间

            notification = builder.build(); // 获取构建好的Notification
            ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(notifyId, notification);
        }
    }

    protected void hideNotify() {
        if (!show) return;
        if (Build.VERSION.SDK_INT >= 26) {
            NotificationUtils notificationUtils = new NotificationUtils(AppContex.getContext());
            notificationUtils.hide(notifyId);
        } else {
            ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancel(notifyId);
        }
    }

    protected void releaseRing() {
        if (ring != null) {
            if (ring.isPlaying()) {
                ring.stop();
            }
            ring = null;
        }
    }

//    public void toFloat() {
//        mIsSmall = true;
//        WindowManager m = getWindowManager();
//        Display d = m.getDefaultDisplay();
//        WindowManager.LayoutParams p = getWindow().getAttributes();
//        Point point = new Point();
//        d.getSize(point);
//        p.height = (int) (point.y * 0.3);
//        p.width = (int) (point.x * 0.3);
//        p.dimAmount = 0.0f;
//        getWindow().setAttributes(p);
//    }
//
//    public void toFixed() {
//        WindowManager.LayoutParams lp = getWindow().getAttributes();
//        lp.x = 0;
//        lp.y = 0;
//        getWindow().setAttributes(lp);
//        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//        mIsSmall = false;
//    }
//
//    protected void bindRootView(View rootView) {
//        rootView.setOnTouchListener((v, event) -> {
//            switch (event.getAction()) {
//                case MotionEvent.ACTION_DOWN:
//                    Logger.d(TAG, "down " + event);
//                    mLastTx = event.getRawX();
//                    mLastTy = event.getRawY();
//                    return true;
//                case MotionEvent.ACTION_MOVE:
//                    Logger.d(TAG, "move " + event);
//                    float dx = event.getRawX() - mLastTx;
//                    float dy = event.getRawY() - mLastTy;
//                    mLastTx = event.getRawX();
//                    mLastTy = event.getRawY();
//                    Logger.d(TAG, "  dx: " + dx + ", dy: " + dy);
//                    if (mIsSmall) {
//                        WindowManager.LayoutParams lp = getWindow().getAttributes();
//                        lp.x += dx;
//                        lp.y += dy;
//                        getWindow().setAttributes(lp);
//                    }
//
//                    break;
//                case MotionEvent.ACTION_UP:
//                    Logger.d(TAG, "up " + event);
//                    return true;
//                case MotionEvent.ACTION_CANCEL:
//                    Logger.d(TAG, "cancel " + event);
//                    return true;
//            }
//            return false;
//        });
//    }
}
